package com.jtjrenren.android.taxi.passenger.manger.push.cmd;

import com.jtjrenren.android.taxi.passenger.bean.Order;
import com.jtjrenren.android.taxi.passenger.manger.AppManger;
import com.jtjrenren.android.taxi.passenger.utils.ModuleUtils;
import com.library.utils.ConvertUtils;
import com.library.utils.StringUtils;


/**
 * Created by Administrator on 2016/1/13.
 */
public class ControlCmd extends Protocl {
    /**
     * 获取消息体流水号,进行加锁处理
     *
     * @return
     */
    protected synchronized static int getSerialNo() {
        AppManger.getInstance().serial_no = (AppManger.getInstance().serial_no + 1) % 9999;
        return AppManger.getInstance().serial_no;
    }

    /**
     * 获取联系方式
     *
     * @return
     */
    protected static String getAccount() {
        String account = "";
        if (AppManger.getInstance().user != null)
            account = AppManger.getInstance().user.getAccount();
        if (StringUtils.isEmpty(account))
            account = "13554943982";
        return account;
    }

    /**
     * 回复通用应答
     *
     * @param centerMsgSerNo 对应的中心消息的流水号(两个字节 封装好后的)
     * @param centerMsgId    对应的中心消息的ID
     * @param result         0：成功/确认；1：失败；2：消息有误；
     * @param orderSerialID  电召单号 没有封装的
     * @return
     */
    public static String getCommnResponseCmd(String centerMsgSerNo, String centerMsgId, int result, String orderSerialID) {
        String reResult = fillingZero(result + "", 2, false);
        String orderSerId = getOrderSerialNo(orderSerialID);

        StringBuilder content = new StringBuilder();
        content.append(centerMsgSerNo).append(centerMsgId)
                .append(reResult).append(orderSerId);

        return getCmd(MSG_ID_COMMONRESPONSE, content.toString());
    }

    /**
     * 上传召车信息
     *
     * @return
     */
    public static String getUploadOrderCmd(Order order) {
        String orderType = "00";
        if (!StringUtils.isEmpty(order.getBookDate()))
            orderType = "01";

        String orderCarType = fillingZero(order.getOrderCarType() + "", 2, false);

        String orderStartTime = StringUtils.isEmpty(order.getCreateDate()) ? fillingZero("", 12, false) :
                ConvertUtils.bytes2hex(ConvertUtils.str2Bcd(order.getCreateDate()));//下单时间

        String startLng = latLngToUINT32(order.getStartLng());  //起始经度
        String startLat = latLngToUINT32(order.getStartLat());  //起始维度
        String carNo = fillingZero("", 20, true);//车牌号
        String carType = fillingZero(Integer.toHexString(order.getCarType()), 2, false); //车型
        String advanceDate = StringUtils.isEmpty(order.getBookDate()) ? fillingZero("", 12, false) :
                ConvertUtils.bytes2hex(ConvertUtils.str2Bcd(order.getBookDate()));//预约用车时间
        String recordAudioId = fillingZero("", 18, false);  //录音文件id
        String endLng = latLngToUINT32(order.getEndLng());  //目的地经度
        String endLat = latLngToUINT32(order.getEndLat());  //目的地维度
        String passgerPhone = getString(order.getPassgerPhone());//乘客电话
        String tip = getString(ModuleUtils.DesEncry(order.getOrderTip()));//电召服务费
        String startAddr = getString(order.getStartAddr());//出发地名
        String endAddr = getString(order.getEndAddr());//目的地地名
        String remark = getString(order.getRemark());//备注


        StringBuilder sb = new StringBuilder();
        sb.append(orderType).append(orderCarType).append(orderStartTime).append(startLng).append(startLat)
                .append(carNo).append(carType).append(advanceDate).append(recordAudioId)
                .append(endLng).append(endLat).append(passgerPhone).append(tip).append(startAddr)
                .append(endAddr).append(remark);


        return getCmd(MSG_ID_UPORDER, sb.toString());
    }

    /**
     * 上传取消电召
     *
     * @param reason
     * @return
     */
    public static String getCancleOrderCmd(String orderSerial, int reason) {

        String orderSerialID = getOrderSerialNo(orderSerial); //电召单号
        String cancleReason = Integer.toHexString(reason); //取消原因
        if (cancleReason.length() == 1) {
            cancleReason = "0" + cancleReason;
        }

        StringBuilder sb = new StringBuilder();
        sb.append(orderSerialID).append(cancleReason);

        return getCmd(MSG_ID_ORDERCANCLE, sb.toString());

    }

    /**
     * 上传支付结果
     */
    public static String getUploadPayResultCmd(String orderSerno, String money, boolean isPaySuccess) {
        String orderSerialID = getOrderSerialNo(orderSerno);
        String payresult = isPaySuccess ? "01" : "00";
        String remark = getString(money);

        StringBuilder sb = new StringBuilder();
        sb.append(orderSerialID).append(payresult).append(remark);

        return getCmd(MSG_ID_UPORDERPAYRESULT, sb.toString());
    }

    /**
     * 心跳包
     *
     * @return
     */
    public static String getHeartCmd() {
        return getCmd(MSG_ID_HEART, null);
    }

    /**
     * 获取协议头
     *
     * @param msgID   消息ID
     * @param account 账号
     * @return
     */
    protected static String getHeader(String msgID, String account) {
        StringBuilder sb = new StringBuilder();

        sb.append(msgID).append(getAccountEncap(account))
                .append(getSerialNumber(getSerialNo()));

        return sb.toString();
    }

    /**
     * 将协议封装
     *
     * @param msgID   消息ID (转换后的)
     * @param content 具体消息体内容 (转换后的)
     */
    protected static String getCmd(String msgID, String content) {
        String account = getAccount();

        StringBuilder sb = new StringBuilder();
        sb.append(getHeader(msgID, account));
        if (content != null && !"".equals(content))
            sb.append(content);
        sb.append(ConvertUtils.byteToHexStr(getProtoclCheckCode(ConvertUtils.HexStrToByte(sb.toString()))));

        String contentTranBefore = sb.toString();
        String contentTran = getTransferredProtocl(contentTranBefore);

        return (PROTOCOL_FLAG + contentTran + PROTOCOL_FLAG).toUpperCase();
    }

}
